<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="5" :sm="24">
              <a-form-item label="">
                <a-month-picker  :default-value="moment().format('YYYY-MM')" style="width: 100%" placeholder="请选择时间" @change="onChangerentedDay"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="">
                <a-select v-model="queryParam.estateId" style="width: 100%">
                  <a-select-option  style="width: 100%"  :key="0" :value="0">全部房產</a-select-option>
                  <a-select-option  style="width: 100%"  v-for="(item,index) in stateList" :key="index" :value="item.id">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="">
                 <a-select v-model="queryParam.type" style="width: 100%">
                  <a-select-option  style="width: 100%"  :key="0" :value="0">全部類型</a-select-option>
                  <a-select-option  style="width: 100%"  v-for="(item,index) in typeList" :key="index" :value="item.id">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="7" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <div>
        <a-button type="primary"  @click="$refs.choBiaoForm.edit()">集中抄錶</a-button>
        <a-button type="primary"  @click="$refs.huanBiaoForm.edit()">批量換表</a-button>
      </div>
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
      </s-table>
      <cho-biao-form ref="choBiaoForm" @ok="handleOk"/>
      <huan-biao-form ref="huanBiaoForm" @ok="handleOk"/>
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
  import moment from 'moment'
  import { STable } from '@/components'
  import { hydropowerMeterPage, hydropowerMeterDelete } from '@/api/modular/main/hydropowermeter/hydropowerMeterManage'
  import { getAllStateList } from '@/api/modular/main/busrealestate/busRealEstateManage'
  import { busCostBillHydropowerList } from '@/api/modular/main/buscostbillhydropower/busCostBillHydropowerManage'
  import choBiaoForm from './choBiaoForm.vue'
  import editForm from './editForm.vue'
  import huanBiaoForm from './huanBiaoForm.vue'
  export default {
    components: {
      STable,
      choBiaoForm,
      editForm,
      huanBiaoForm
    },
    data () {
      return {
        // 查询参数
        queryParam: {
          meterReadingDate: moment().format('YYYY-MM')
        },
        // 表头
        columns: [
          {
            title: '房間',
            align: 'center',
            dataIndex: 'estateRoomName'
          },
          {
            title: '租客',
            align: 'center',
            dataIndex: 'reservation'
          },
          {
            title: '類型',
            align: 'center',
            dataIndex: 'typeName'
          },
          {
            title: '读数',
            align: 'center',
            dataIndex: 'reading'
          },
          {
            title: '用量',
            align: 'center',
            dataIndex: 'dosage'
          },
          {
            title: '抄表日期',
            align: 'center',
            dataIndex: 'meterReadingDate'
          },
          {
            title: '费用',
            align: 'center',
            dataIndex: 'cost'
          },
          {
            title: '收租日',
            align: 'center',
            dataIndex: 'rentedDay'
          },
          {
            title: ' 状态',
            align: 'center',
            dataIndex: 'meterStatus'
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return hydropowerMeterPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        stateList: [],
        typeList: []
      }
    },
    created () {
      this.getStateList()
      this.getCostBillHydropowerList()
    },
    methods: {
      moment,
      getStateList() {
        getAllStateList().then((res) => {
          if (res.code === 200) {
            this.stateList = res.data
          } else {
            this.$message.error(res.msg) // + res.message
          }
        })
      },
      getCostBillHydropowerList() {
        busCostBillHydropowerList().then((res) => {
          if (res.code === 200) {
            this.typeList = res.data
          } else {
            this.$message.error(res.msg) // + res.message
          }
        })
      },
      hydropowerMeterDelete (record) {
        hydropowerMeterDelete(record).then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      onChangerentedDay(date, dateString) {
        this.queryParam.meterReadingDate = dateString
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
